﻿/* 
 *  <copyright file="Base3DGraphic.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using SobrietyEngine.Actor;
using SobrietyEngine.Common;
using SobrietyEngine.Components.SceneGraph;

namespace SobrietyEngine.Components.Graphic
{
    abstract public class Base3DGraphic : BaseComponent, IGraphic3D, IComponent, IGraphicVisibility
    {
        protected int boundingRadius = -1;
        public int BoundingRadius
        {
            get
            {
                if (boundingRadius < 0)
                    throw new Exception("Bounding radius not set");
                return boundingRadius;
            }
            private set { boundingRadius = value; }
        }

        protected Boolean alwaysDraw = false;
        public Boolean AlwaysDraw { get { return alwaysDraw; } set { alwaysDraw = value; } }

        public Boolean Visible { get; set; }

        ISceneNode sceneNode;
        Boolean hasSceneNode = false;

        public Base3DGraphic()
            : base()
        {
            Visible = true;
        }

        public override bool InitComponent()
        {
            base.InitComponent();
            SetSceneNode(Actor.Get<ISceneNode>());
            return true;
        }

        public void SetSceneNode(ISceneNode node)
        {
            sceneNode = node;
            hasSceneNode = sceneNode != null ? true : false;
        }

        public override void Set(Parameters parms)
        {
            Visible = parms.GetValueAs<Boolean>("Visible", true);
            AlwaysDraw = parms.GetValueAs<Boolean>("AlwaysDraw", false);
        }

        public override List<ParameterDescription> GetParameterDescriptions()
        {
            List<ParameterDescription> p = base.GetParameterDescriptions();
            p.Add(new ParameterDescription("Visible", "If the graphic is drawn or not", typeof(Boolean), true));
            p.Add(new ParameterDescription("AlwaysDraw", "If the graphic is always drawn, weather on camera or not", typeof(Boolean), false));

            p.Add(new ParameterDescription("AttachedTo", "Point on actor the graphic is attached to", typeof(Vector3), Vector3.Zero));
            p.Add(new ParameterDescription("Offset", "Offset from AttachedTo to AttachedAt", typeof(Vector3), Vector3.Zero));
            p.Add(new ParameterDescription("AttachedAt", "Point on the graphic that is attached to actor", typeof(Vector3), Vector3.Zero));
            p.Add(new ParameterDescription("RespectRotaton", "If we respect the rotation of the actor", typeof(Boolean), false));
            return p;
        }

        override public Type[] SupportedInterfaces()
        {
            return new Type[] { typeof(IGraphic3D) };
        }

        abstract public void Draw(ICamera _camera, Matrix _projection, Matrix _view, Matrix _world);

        public void Draw(ICamera cameraActor, Matrix projection, Matrix view)
        {
            if (hasSceneNode)
                Draw(cameraActor, projection, view, sceneNode.GetAbsoluteParentTransformation());
            else
                Draw(cameraActor, projection, view, Matrix.Identity);
        }

        public abstract void UpdateGraphic(GameTime time);
        
    }
}
